"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : gs_guc配置dcf_config参数带转义符
Create At   : 2022/06/07
Owner       : opentestcase023
Description :
    1.gs_guc配置dcf_config参数带转义符
    3.清理环境
Expect      :
    1.配置成功
    2.恢复dcf_config为空
History     :
"""
import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger

COMMONSH = CommonSH("PrimaryDbUser")


@unittest.skipIf(3 != COMMONSH.get_node_num(), '非1+2环境不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.pri_comsh = CommonSH("PrimaryDbUser")
        self.primary_node = Node('PrimaryDbUser')
        self.standby1_node = Node('Standby1DbUser')
        self.standby2_node = Node('Standby2DbUser')
        self.pri_host = self.primary_node.db_host
        self.sta1_host = self.standby1_node.db_host
        self.sta2_host = self.standby2_node.db_host
        self.port = self.primary_node.db_port
        self.u_name = self.primary_node.ssh_user

    def test_gs_guc(self):
        text = '-----step1:gs_guc配置dcf_config参数带转义符 expect:成功-----'
        self.log.info(text)
        dcf_config = f'[{{{{\\"stream_id\\":1,\\"node_id\\":1,' \
                     f'\\"ip\\":\\"{self.pri_host}\\",' \
                     f'\\"port\\":{self.port},\\"role\\":\\"LEADER\\"}}}},' \
                     f'{{{{\\"stream_id\\":1,\\"node_id\\":2,' \
                     f'\\"ip\\":\\"{self.sta1_host}\\",' \
                     f'\\"port\\":{self.port},\\"role\\":\\"FOLLOWER\\"}}}},' \
                     f'{{{{\\"stream_id\\":1,\\"node_id\\":3,' \
                     f'\\"ip\\":\\"{self.sta2_host}\\",' \
                     f'\\"port\\":{self.port},\\"role\\":\\"FOLLOWER\\"}}}}]'
        self.log.info(dcf_config)
        guc_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -D {macro.DB_INSTANCE_PATH} -c ' \
                  f'"dcf_config = \'{dcf_config}\'";'
        self.log.info(guc_cmd)
        guc_res = self.common.get_sh_result(self.primary_node, guc_cmd)
        self.log.info(guc_res)
        self.assertIn('Success to perform gs_guc', guc_res, 'guc设置错误')
        self.pri_comsh.restart_db_cluster()
        check_res = self.pri_comsh.execut_db_sql('show dcf_config;')
        self.log.info(check_res)
        dcf_check = f'[{{"stream_id":1,"node_id":1,"ip":"{self.pri_host}",' \
                    f'"port":{self.port},"role":"LEADER"}},' \
                    f'{{"stream_id":1,"node_id":2,"ip":"{self.sta1_host}",' \
                    f'"port":{self.port},"role":"FOLLOWER"}},' \
                    f'{{"stream_id":1,"node_id":3,"ip":"{self.sta2_host}",' \
                    f'"port":{self.port},"role":"FOLLOWER"}}]'
        self.assertIn(dcf_check, check_res, 'guc参数设置错误')

    def tearDown(self):
        text = "-----step4:恢复环境 expect:成功-----"
        self.log.info(text)
        guc_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -D {macro.DB_INSTANCE_PATH} -c ' \
                  f'"dcf_config = \'\'"'
        self.log.info(guc_cmd)
        guc_res = self.common.get_sh_result(self.primary_node, guc_cmd)
        self.log.info(guc_res)
        self.pri_comsh.restart_db_cluster()
        check_res = self.pri_comsh.execut_db_sql('show dcf_config;')
        self.log.info(check_res)
        self.assertTrue(2 <= len(check_res), '获取参数失败')
        self.assertEqual('', check_res.splitlines()[-2].strip(), 'guc参数设置错误')
        self.log.info(f'-----{os.path.basename(__file__)} finish-----')
